#include "head.h"


// 显示菜单并获取用户选择
int display_menu(const wchar_t *title, const wchar_t *choices[], int n_choices) {
    // 设置语言环境，确保可以处理多字节字符（如中文）
    setlocale(LC_ALL, "");

    // 初始化 ncurses 库
    initscr();
    clear();
    noecho();
    cbreak();
    curs_set(0);

    int highlight = 1;
    int choice = 0;
    int c;  // 使用int而不是char，以处理ncurses中的特殊键值

    // 创建窗口用于显示菜单，设置窗口大小为全屏
    WINDOW *menu_win = newwin(LINES, COLS, 0, 0);
    keypad(menu_win, TRUE);
    
    update_menu(menu_win, title, highlight, choices, n_choices); // 显示菜单

    // 菜单交互循环
    while(1) {
        c = wgetch(menu_win);
        switch(c) {
            case KEY_UP:  
                highlight = (highlight == 1) ? n_choices : highlight - 1;
                break;
            case KEY_DOWN: 
                highlight = (highlight == n_choices) ? 1 : highlight + 1;
                break;
            case 10:  // Enter 键
                choice = highlight;
                break;
            default:
                break;
        }
        update_menu(menu_win, title, highlight, choices, n_choices);
        if(choice != 0)  // 如果按了回车键，则退出循环
            break;
    }

    // 结束 ncurses 模式
    endwin();

    return choice; // 返回用户选择的选项值
}

// 更新并显示菜单
void update_menu(WINDOW *menu_win, const wchar_t *title, int highlight, const wchar_t *choices[], int n_choices) {
    int x, y;

    // 计算标题的水平居中位置，并稍微向上移动
    int title_y = 2;  // 标题的位置固定在上面
    int title_x = ((COLS - wcslen(title) - (wcslen(title) / 4)) / 2) - 2;  // 计算标题的水平居中位置，留出 * * 的位置

    // 绘制标题及其修饰
    for (size_t i = 0; i < COLS; i++) {
        mvwprintw(menu_win, title_y - 1, i, "*", title);
        mvwprintw(menu_win, title_y + 1, i, "*", title);
    }
    
    mvwprintw(menu_win, title_y, title_x, " %ls ", title);

    // 计算菜单项的起始行位置，确保其在标题下方居中显示
    int start_y = (LINES - n_choices) / 2;

    // 绘制菜单项
    box(menu_win, 0, 0);  
    for(int i = 0; i < n_choices; ++i) {
        y = start_y + i;  // 逐行打印菜单项
        x = (COLS - wcslen(choices[i])) / 2;  // 计算每个菜单项的水平居中位置

        if(highlight == i + 1) {
            wattron(menu_win, A_REVERSE | A_BOLD);  // 高亮并加粗当前选项
            mvwprintw(menu_win, y, x, "%ls", choices[i]);
            wattroff(menu_win, A_REVERSE | A_BOLD);
        } else {
            mvwprintw(menu_win, y, x, "%ls", choices[i]);
        }
    }

    wrefresh(menu_win);
}
void start(void)
{
  //setlocale(LC_ALL, "C");
  setlocale(LC_ALL, "");
  const char *art[] = {
      "    ______                                                                     ",
      "    L,.   ',                                                                   ",
      "     \\      ',_                            ",
      "      \\   @   ',                                                               ",
      "       \\ ^~^    ',                                                             ",
      "        \\         ',                                                           ",
      "         \\___'____  ',_                          _..----.._                    ",
      "         [______       \"'==.I\\_____________..--\"<__\\_n@___4\\,_                ",
      "       ,..-=T         __   ____________          \\/  \"'\" 0<==  \"'-+.._         ",
      "       I____|_____    }_>=========I>=**\"\"''==-------------==-   \" |   \"'-.,___",
      "     [_____,.--'\"                             \"\"--=<\"\"-----=====+==--''\"\"      ",
      "       \"'-=+..,,__,-----,_____                  -=* |                         ",
      "                   |__   /-----'#------.,I_---------'\"                         ",
      "                      \"\"\"\"'\"--..__         _.>                                  ",
      "                                 \"\"\"\"\"\"\"\"        "};
  initscr();   // Initialize the screen
  cbreak();    // Disable line buffering
  noecho();    // Don't echo input
  curs_set(0); // Hide the cursor

  int max_y, max_x;
  getmaxyx(stdscr, max_y, max_x); // Get the screen dimensions

  int art_height = sizeof(art) / sizeof(art[0]);
  int art_width = 0;
  for (int i = 0; i < art_height; i++)
  {
    if (strlen(art[i]) > art_width)
    {
      art_width = strlen(art[i]);
    }
  }

  int offset = 0;
  int frame_count = 0;
  const int max_frames = 100; // Number of frames to display the animation

  while (frame_count < max_frames)
  {
    clear(); // Clear the screen
    for (int i = 0; i < art_height; i++)
    {
      mvprintw(max_y / 2 + i - art_height / 2, offset, "%s", art[i]);
    }
    refresh();                                   // Refresh the screen
    offset = (offset + 1) % (max_x + art_width); // Move the art to the right
    usleep(6000);                              // Sleep for 30 milliseconds
    frame_count++;
  }
  setlocale(LC_ALL, "C");
  endwin(); // End ncurses
}